﻿using AvalonDock;
using AvalonDock.Layout;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using 雷赛板卡上位机.Models;
using 雷赛板卡上位机.Services;
using 雷赛板卡上位机.View;
using TabItem = HandyControl.Controls.TabItem;

namespace 雷赛板卡上位机.ViewModel
{
    public partial class TabControlViewModel : BaseViewModel
    {
        /// <summary>
        /// 观察属性，用于存储标签页集合
        /// </summary>
        [ObservableProperty]
        ObservableCollection<LayoutDocument> dock = new ObservableCollection<LayoutDocument>();

        /// <summary>
        /// 观察属性，用于存储当前选中的标签页
        /// </summary>
        [ObservableProperty]
        LayoutDocument dockSelectedItem = default(LayoutDocument);

        /// <summary>
        /// 观察属性，用于存储当前选中的 TabItem
        /// </summary>
        [ObservableProperty]
        TabItem selectedItem = default(TabItem);


        /// <summary>
        /// 当标签页关闭时的处理方法
        /// </summary>
        /// <param name="sender">关闭的标签页</param>
        private void Close(LayoutDocument sender)
        {
            if (sender is not null)
            {
                if (sender is LayoutDocument layoutDocument)
                {
                    switch (layoutDocument?.Content)
                    {
                        case AxisControlViewModel viewModel:
                            viewModel.Dispose();
                            break;
                    }
                }
            }
        }

        /// <summary>
        /// 移除标签页事件处理方法
        /// </summary>
        /// <param name="sender">事件发送者</param>
        [RelayCommand]
        private void Remove(EventArgs sender)
        {
            if (sender is DocumentClosedEventArgs e)
                if (e.Document.Content is LayoutDocument document)
                {
                    Close(document);
                    Dock.Remove(document);
                }
        }

        /// <summary>
        /// 激活视图模型时调用的方法
        /// </summary>
        protected override void OnActivated()
        {
            // 注册消息，当接收到 LayoutDocument 和字符串消息时，处理逻辑如下
            Messenger?.Register<LayoutDocument, string>(this, nameof(TabItem), (r, m) =>
            {
                if (m is LayoutDocument layoutDocument)
                {
                    if (layoutDocument.Content is AxisControlViewModel viewModel)
                    {
                        viewModel.IsStart = true;
                    }

                    // 遍历已有的标签页，检查是否已经存在相同标题的标签页
                    for (var i = 0; i < dock.Count; i++)
                    {
                        if (dock[i] is LayoutDocument tabItem)
                        {
                            if (tabItem.Title == m.Title)
                            {
                                this.DockSelectedItem = dock[i];
                                return;
                            }
                        }
                    }
                    Dock.Add(m);
                    DockSelectedItem = m;
                }
            });

            // 注册消息，当接收到 TabControlViewModel 和 RequestMessage<InputTextBoxViewModel> 消息时，处理逻辑如下
            Messenger?.Register<TabControlViewModel, RequestMessage<InputTextBoxViewModel>>(this, (r, m) =>
            {
                foreach (var item in Dock)
                {
                    if (item.Content is InputTextBoxViewModel inputTextBox)
                    {
                        DockSelectedItem = item;
                        m.Reply(inputTextBox);
                        return;
                    }
                }
                InputTextBoxViewModel? req = ControlService.GetInputTextBoxViewModel();

                LayoutDocument layoutDocument = new LayoutDocument { Content = req, Title = "代码执行区" };
                Dock.Add(layoutDocument);
                DockSelectedItem = layoutDocument;
                m.Reply(req);
            });

            // 注册消息，当接收到 TabControlViewModel、ValueChangedMessage<bool> 和字符串消息时，处理逻辑如下
            Messenger?.Register<TabControlViewModel, ValueChangedMessage<bool>, string>(this, "MenuClose", (r, m) =>
            {
                if (m.Value)
                {
                    r.Dock.Clear();
                }
            });
        }
    }
}
